﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SerieInterface;
using MyInteractiveSerieList.Properties;
using System.IO;

namespace MyInteractiveSerieList
{
    public class IOControl : IIOControl
    {

        public void Save(string season, string episode, string serie)
        {
            List<string> listOfSeries = new List<string>();
            StringBuilder sb = new StringBuilder();

            string stringOfSeries = GetCorrectSerieString(serie, "load");

            NextSeasonEpisode(season, episode, serie, out season, out episode);

            if (String.IsNullOrEmpty(season) || string.IsNullOrEmpty(episode))
                return;

            string toBeInserted = (serie + "-S" + season + "E" + episode).Replace(" ", "");

            bool exist = false;

            foreach (string s in stringOfSeries.Split(';'))
            {
                if (!string.IsNullOrEmpty(s) && s.Length > serie.Length)
                    if (string.Compare(s.Substring(0, serie.Replace(" ", "").Length), serie.Replace(" ", ""), true) == 0)
                    {
                        listOfSeries.Add(toBeInserted);
                        exist = true;
                    }
                    else
                        listOfSeries.Add(s);
                else if (!string.IsNullOrEmpty(s))
                    listOfSeries.Add(s);
            }

            if (!exist)
                listOfSeries.Add(toBeInserted);

            listOfSeries.Sort();

            foreach (string s in listOfSeries)
            {
                sb.Append(s);
                sb.Append(';');
            }
            stringOfSeries = sb.ToString();
            GetCorrectSerieString(serie, "save", stringOfSeries);

        }

        public string Load(string serie)
        {
            string stringOfSeries = GetCorrectSerieString(serie, "load");

            foreach (string StringOfSerie in stringOfSeries.Split(';'))
            {
                if (StringOfSerie.Length == (serie.Replace(" ", "").Length + "-S00E00".Length))
                    if (StringOfSerie.IndexOf(serie.Replace(" ", "")) != -1)
                        return StringOfSerie;
            }

            return serie.Replace(" ", "") + "-S01E00";
        }


        public bool DoesNextExist(string season, string episode, string serie)
        {
            string _episode, _season;

            NextSeasonEpisode(season, episode, serie, out _season, out _episode);

            if (string.Compare(season, _season, true) == 0 && string.Compare(episode, _episode, true) == 0)
                return false;
            else
                return true;
        }

        public void NextSeasonEpisode(string season, string episode, string serie, out string _season, out string _episode)
        {
            if (season.Length < 2)
                season = "0" + season;
            if (episode.Length < 2)
                episode = "0" + episode;

            _episode = null; string originalE = episode;
            _season = null; string originalS = season;
            foreach (string path in Settings.Default.PathToSeries.Split(';'))
            {
                if (Directory.Exists(path + "\\" + serie))
                {
                    DirectoryInfo[] dI = new DirectoryInfo(path + "\\" + serie).GetDirectories("S" + season);
                    int iEpisode = int.Parse(episode) + 1;
                    FileInfo[] fI = dI[0].GetFiles();
                    if (iEpisode <= fI.Count())
                    {
                        if (iEpisode < 10)
                        {
                            _episode = "0" + iEpisode;
                            _season = season;
                        }
                        else
                        {
                            _episode = iEpisode.ToString();
                            _season = season;
                        }
                        break;
                    }
                    else
                    {
                        int iSeason = int.Parse(season) + 1;
                        if (iSeason < 10)
                            season = "0" + iSeason;
                        else
                            season = iSeason.ToString();

                        dI = new DirectoryInfo(path + serie).GetDirectories("S" + season);
                        if (dI.Length > 0)
                        {
                            _season = season;
                            _episode = "01";
                        }
                        else
                        {
                            _season = originalS;
                            _episode = originalE;
                        }
                    }

                }
            }
        }

        private string GetCorrectSerieString(string serie, string Switch, string stringOfSeries = null)
        {
            string abc = "abcdefghijklmnopqrstuwvxyzåäö".ToUpper();
            int seriePlace = abc.IndexOf(serie[0]);

            switch (Switch.ToLower())
            {
                case "load":
                    if (seriePlace <= abc.IndexOf('G'))
                        stringOfSeries = Settings.Default.SeriesAtoG;
                    else if (seriePlace <= abc.IndexOf('N'))
                        stringOfSeries = Settings.Default.SeriesHtoN;
                    else if (seriePlace <= abc.IndexOf('U'))
                        stringOfSeries = Settings.Default.SeriesOtoU;
                    else
                        stringOfSeries = Settings.Default.SeriesWtoZ;
                    return stringOfSeries;

                case "save":
                    if (seriePlace <= abc.IndexOf('G'))
                        Settings.Default.SeriesAtoG = stringOfSeries;
                    else if (seriePlace <= abc.IndexOf('N'))
                        Settings.Default.SeriesHtoN = stringOfSeries;
                    else if (seriePlace <= abc.IndexOf('U'))
                        Settings.Default.SeriesOtoU = stringOfSeries;
                    else
                        Settings.Default.SeriesWtoZ = stringOfSeries;
                    Settings.Default.Save();
                    break;

                default:
                    break;
            }
            return null;


        }
    }
}
